﻿using Sharp.Infrastructure;
using Sharp.Portal.Owin;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Sharp.Portal.Mvc.List
{
    /// <summary>
    /// 自定义列信息结构
    /// </summary>
    public class CustomColumnItem
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CustomColumnItem()
        {
            this.Name = string.Empty;
            this.Width = -1;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        public CustomColumnItem(string name)
            : this()
        {
            this.Name = name;
        }
        /// <summary>
        /// 绑定属性名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 隐藏
        /// </summary>
        public Boolean Hidden { get; set; }
        /// <summary>
        /// 列宽度
        /// </summary>
        public int Width { get; set; }
        /// <summary>
        /// 查询
        /// </summary>
        public Boolean Query { get; set; }

    }
    /// <summary>
    /// 自定义列信息
    /// </summary>
    public class CustomColumns
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CustomColumns()
        {
            Columns = new List<CustomColumnItem>();
            this.SortByColumn = string.Empty;
        }
        /// <summary>
        /// 排序列
        /// </summary>
        public string SortByColumn { get; set; }
        /// <summary>
        /// 排序方向
        /// False: ASC; True: DESC
        /// </summary>
        public Boolean Sorting { get; set; }

        /// <summary>
        /// 列信息
        /// </summary>
        public List<CustomColumnItem> Columns { get; set; }

        /// <summary>
        /// 根据字段名查找列信息索引
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int IndexOf(string name)
        {
            for (int i = 0; i < this.Columns.Count; i++)
                if (this.Columns[i].Name == name)
                    return i;
            return -1;
        }
        /// <summary>
        /// 从文件中载入配置
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        public Boolean LoadFromFile(int userId, string moduleId)
        {
            FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath(string.Format("~\\App_Data\\User\\{0}\\CustomColumns_{1}.config", userId, moduleId)));
            if (!fi.Exists)
            {
                return false;
            }

            CustomColumns config;
            try
            {
                config = SerializableHelper.DeSerialize<CustomColumns>(fi);
            }
            catch
            {
                return false;
            }
            this.SortByColumn = config.SortByColumn;
            this.Sorting = config.Sorting;

            foreach (var t in config.Columns)
            {
                this.Columns.Add(t);
            }
            return true;
        }

        /// <summary>
        /// 保存配置到文件
        /// </summary>
        /// <param name="moduleId"></param>
        public void SaveToFile(string moduleId)
        {
            string path =
                HttpContext.Current.Server.MapPath(string.Format("~\\App_Data\\User\\{0}\\",
                    SharpUserManager.Current.Id));
            DirectoryInfo di = new DirectoryInfo(path);
            if (!di.Exists)
                di.Create();
            string file = string.Format("{0}CustomColumns_{1}.config", path, moduleId);
            SerializableHelper.Serialize(this, file);

            string key = string.Format("CustomColumns_{0}_{1}", moduleId, SharpUserManager.Current.Id);

            DicCustomColumns.AddOrUpdate(key, this, (x, y) => y);
        }
        /// <summary>
        /// 配置文件
        /// </summary>
        public static readonly ConcurrentDictionary<string, CustomColumns> DicCustomColumns =
            new ConcurrentDictionary<string, CustomColumns>();
    }
}
